/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.main.header;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.border.LineBorder;
import org.jfonia.constants.ViewConstants;

/**
 *
 * @author Rik Bauwens
 */
public class HeaderTextField extends IntegerTextField implements MouseListener, FocusListener
{
        private Color backgroundColor;

        public HeaderTextField(String string, int columns)
        {
                super(string, columns, columns);
                setColor(ViewConstants.OPTION_BACKGROUND_COLOR);
                setOpaque(false);
                
                setEditable(false);
                addMouseListener(this);
                addFocusListener(this);
        }

        private void setColor(Color color)
        {
                backgroundColor = color;
                setBorder(new LineBorder(backgroundColor, 3, true));
                repaint();
        }

        @Override
        protected void paintComponent(Graphics g)
        {
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                    RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setColor(backgroundColor);
                g2.fillRoundRect(0, 0, getPreferredSize().width,
                                       getPreferredSize().height,
                                       ViewConstants.ROUNDRECT_ARC_WIDTH,
                                       ViewConstants.ROUNDRECT_ARC_HEIGHT);
                g2.setColor(Color.BLACK);
                super.paintComponent(g);
        }

        public void mouseClicked(MouseEvent e)
        {
                if(isEnabled())
                        setEditable(!isEditable());
        }

        public void mousePressed(MouseEvent e) {}

        public void mouseReleased(MouseEvent e) {}

        public void mouseEntered(MouseEvent e)
        {
                if(!isEditable() && isEnabled())
                        setColor(ViewConstants.OPTION_HIGHLIGHTED_COLOR);
        }

        public void mouseExited(MouseEvent e)
        {
                if(!isEditable() && isEnabled())
                        setColor(ViewConstants.OPTION_BACKGROUND_COLOR);
        }

        public void focusGained(FocusEvent e) {}

        public void focusLost(FocusEvent e)
        {
                setEditable(false);
        }

        @Override
        public void setEditable(boolean b)
        {
                super.setEditable(b);
                if(isEditable())
                {
                        setColor(ViewConstants.OPTION_SELECTED_COLOR);
                        selectAll();
                }
                else
                        setColor(ViewConstants.OPTION_BACKGROUND_COLOR);
        }

}
